package com.netty.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.charset.Charset;

public class NettyBuf02 {
    public static void main(String[] args) {

        //创建bytebuf
        ByteBuf byteBuf = Unpooled.copiedBuffer("hello,world!", Charset.forName("UTF-8"));
        //相关方法
        if (byteBuf.hasArray()){
            byte[] array = byteBuf.array();
            System.out.println(array.toString());//是地址
            System.out.println(new String(array,Charset.forName("utf-8")));//内容
            System.out.println(byteBuf+"<<<<<>>>>>>>>");//对象
            System.out.println(byteBuf.arrayOffset());//0
            System.out.println(byteBuf.readerIndex());//0
            System.out.println(byteBuf.writerIndex());//12
            System.out.println(byteBuf.capacity());//36 由于中文的utf-8编码对应的是3个字节,所以考虑有输入中文时数组不会溢出
            System.out.println(byteBuf.getByte(0));//ASCII码的数值
            System.out.println(byteBuf.readByte());//ASCII码的数值
            System.out.println(byteBuf.readableBytes());//可读范围
            System.out.println(byteBuf.getCharSequence(4,6, Charset.forName("utf-8")));
        }
    }
}
